using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using MaskUserControl;
using TypeDataset;
using DataAccessor;

namespace ApplicationForm
{
    public partial class Form1 : Form
    {
		private Country.CountryDataTable _countryTable;
		private MaskType.MaskTypeDataTable _typeTable;
        public Form1()
        {
            InitializeComponent();
			_countryTable = new Country.CountryDataTable();
			_DBService.GetDataTable("select * from mask_country", _countryTable);
			_typeTable = new MaskType.MaskTypeDataTable();
			_DBService.GetDataTable("select * from mask_type", _typeTable);

            navigationTree1.OnSelectChangeEvent +=
                new MaskUserControl.OnSelectNodeChange(OnSelectChange);
			maskGridView1.OnOpenDetailEvent += new OnOpenMaskDetail(ModifyMaskInfo);

			string applicationFolder = Environment.GetEnvironmentVariable("MaskApplication");
			Debug.Assert(applicationFolder != "" || applicationFolder != null);
			navigationTree1.XMLFile = string.Format("{0}setting\\NodeView.xml", applicationFolder);
			maskGridView1.XMLFile = string.Format("{0}setting\\GridView.xml", applicationFolder);
			maskGridView1.CountryDataSource = _countryTable;
			maskGridView1.MaskTypeDataSource = _typeTable;

        }
		private IMaskQuery _Query = (QueryInterfaceFactory.Instance()).CreateMaskQuery();
		private DBConnectionService _DBService = DBConnectionService.Instance();
		private void OnSelectChange(MaskNagivationNode viewNode)
        {
			MaskMainView.BaseMaskViewDataTable viewTable = _Query.GetMaskView(new MaskQCondition(viewNode));
			maskGridView1.ViewDataSource = viewTable;
        }
		private void ModifyMaskInfo(MaskMainView.BaseMaskViewRow maskInfo)
		{
			DetailFrm maskDetail = new DetailFrm(maskInfo, _countryTable, _typeTable);
			if (maskDetail.ShowDialog() == DialogResult.OK)
			{
				_DBService.UpdateMaskInfo(maskInfo);
			}

		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (maskGridView1.ViewDataSource != null)
			{
				MaskMainView.BaseMaskViewRow row = maskGridView1.ViewDataSource.NewBaseMaskViewRow();
				DetailFrm maskDetail = new DetailFrm(row, _countryTable, _typeTable);
				if (maskDetail.ShowDialog() == DialogResult.OK)
				{
					maskGridView1.ViewDataSource.AddBaseMaskViewRow(row);
					_DBService.UpdateMaskInfo(row);
				}
			}
		}

    }
}